package com.shujia.flink.sql

import org.apache.flink.streaming.api.TimeCharacteristic
import org.apache.flink.streaming.api.scala._
import org.apache.flink.table.api._
import org.apache.flink.table.api.bridge.scala._

object Demo9EventTime {
  def main(args: Array[String]): Unit = {
    //flink  流处理的环境
    val bsEnv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    //本地测试将并行的设置为1，每一个并行度中水位线不一致的
    bsEnv.setParallelism(1)

    //设置时间模式为事件时间
    bsEnv.setStreamTimeCharacteristic(TimeCharacteristic.EventTime)

    //设置flink  sql 配置
    val bsSettings: EnvironmentSettings = EnvironmentSettings
      .newInstance()
      .useBlinkPlanner() // 使用blink 的计划器（解析sql）
      .inStreamingMode()
      .build()

    //创建table 环境
    val bsTableEnv: StreamTableEnvironment = StreamTableEnvironment.create(bsEnv, bsSettings)

    /*
zhangsan,20210806152310
zhangsan,20210806152311
zhangsan,20210806152312
zhangsan,20210806152313
zhangsan,20210806152314
zhangsan,20210806152315
zhangsan,20210806152316
zhangsan,20210806152317
zhangsan,20210806152318
zhangsan,20210806152319
zhangsan,20210806152320
zhangsan,20210806152321
    */
    /**
      * ts AS TO_TIMESTAMP(user_action_time,'yyyyMMddHHmmss') ： 将字符串转换成时间戳类型
      *
      */

    bsTableEnv.executeSql(
      """
        |
        |CREATE TABLE user_actions (
        |  user_name STRING,
        |  user_action_time STRING,
        |  ts AS TO_TIMESTAMP(user_action_time,'yyyyMMddHHmmss'),
        |  WATERMARK FOR ts AS ts - INTERVAL '5' SECOND
        |) WITH (
        |  'connector' = 'kafka',
        | 'topic' = 'users_event_time',
        | 'properties.bootstrap.servers' = 'master:9092',
        | 'properties.group.id' = 'asdsadasd',
        | 'format' = 'csv',
        | 'scan.startup.mode' = 'earliest-offset',
        | 'csv.ignore-parse-errors' = 'true'
        |)
        |
        |
      """.stripMargin)

//      bsTableEnv.executeSql(
//      """
//        |CREATE TABLE print_table(
//        |user_name STRING,
//        |user_action_time STRING,
//        |ts TIMESTAMP(3)
//        |) WITH ('connector' = 'print')
//        |
//    """.stripMargin)
//
//    bsTableEnv.executeSql(
//      """
//        |insert into print_table
//        |select * from user_actions
//        |
//      """.stripMargin)

    bsTableEnv.executeSql(
      """
        |CREATE TABLE mysql_sink(
        |user_name STRING,
        |end_time TIMESTAMP(3),
        |c BIGINT,
        |PRIMARY KEY (user_name,end_time) NOT ENFORCED
        |) WITH (
        |   'connector' = 'jdbc',
        |   'url' = 'jdbc:mysql://master:3306/test',
        |   'table-name' = 'users_event_time',
        |   'username'='root',
        |   'password'='123456'
        |)
        |
    """.stripMargin)

    bsTableEnv.executeSql(
      """
        |insert into mysql_sink
        |select
        |user_name,
        |TUMBLE_END(ts, INTERVAL '5' SECOND) as end_time,
        |count(1) as c
        |from
        |user_actions
        |group by user_name, TUMBLE(ts, INTERVAL '5' SECOND)
        |
      """.stripMargin)
  }
}
